#include "ViewControl.h"

float glbMouseX = -1.0f;
float glbMouseY = -1.0f;

ViewControl::ViewControl (Ptr< Terrain > pTerrain,
                          Ptr< BattleStatus > pBattleStatus,
                          Ptr< list< Ptr< Object > > > pObjList)
                          : GlControl (), _pBattleStatus (pBattleStatus)
{
  // Create showlist manager
  _pShowListManager = new ShowListManager ();

  // Create terrain model
  Ptr< TerrainModel > pTerrainModel 
    = new TerrainModel (pTerrain, _pShowListManager);

  // Create object model
  Ptr< vector< Model > > pModels = new vector< Model > ();
  Model army (_pShowListManager);
  pModels->push_back (army);

  // Read coord space info
  FileStreamIn input (SCENE_COORD_DATA_FILE);
  if (!input.check_file_type (DATA_FILE_TYPE))
    throw ("File type error.");
  SceneCoordSpaceInfo coordSpaceInfo (input);
  input.close ();

  // Create string painter
  _pStringPainter = new myglStringControl (L"Times New Roman", 20,
    _pShowListManager);

  // Create scene
  _pScene = new Scene (pTerrainModel, _pStringPainter,
    pObjList, pModels,  _pBattleStatus, coordSpaceInfo);

  // Set timer
  set_timer_duration (20);
  set_timer_init_val (0);
}



void ViewControl::init_render ()
{

  glEnable(GL_DEPTH_TEST);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_NORMALIZE);
  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glShadeModel(GL_SMOOTH);
  glEnable(GL_LINE_SMOOTH);
  glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
  //glEnable(GL_POLYGON_SMOOTH);
  //glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

  glClearColor (0.0f, 0.0f, 0.0f, 1.0f);

}


void ViewControl::handle_keypress (unsigned char key, int x, int y)
{
  switch (key) {
    case 27: //Escape key
      exit(0);
  }
}

void ViewControl::draw ()
{
  _pBattleStatus->sun_move ();
  if (_pBattleStatus->time_minus ())
    _pBattleStatus->shift ();
  if (_pBattleStatus->time_minus ())
    _pBattleStatus->shift ();
  if (_pBattleStatus->time_minus ())
    _pBattleStatus->shift ();
  _pScene->draw ();
  _draw_ui ();
}


void ViewControl::_draw_ui ()
{
  glPushMatrix ();
  glPushAttrib (GL_ENABLE_BIT);
  glPushAttrib (GL_CURRENT_BIT);
  glDisable (GL_LIGHTING);

  // Draw time line
  float timelineLength = 0.5f;
  float timelineLengthLeft = timelineLength * _pBattleStatus->time_left ()
    / float (_pBattleStatus->total_time ());
  float timelineWidth = 0.05f;
  float timelineZ = -2.0f;
  float timelineBottom = -0.5f;
  float timelineLeft = -timelineLength / 2;

  glTranslatef (0.0f, timelineBottom, timelineZ);
  // Draw solid fill
  glBegin (GL_QUADS);

  glColor3fv ((_pBattleStatus->cur_player ().color * 0.3).data ());
  glVertex3f (timelineLeft, 0, 0.0f);
  glVertex3f (timelineLeft + timelineLengthLeft, 0, 0.0f);

  glColor3fv (_pBattleStatus->cur_player ().color.data ());
  glVertex3f (timelineLeft + timelineLengthLeft, timelineWidth, 0.0f);
  glVertex3f (timelineLeft, timelineWidth, 0.0f);

  glEnd ();

  // Draw outline
  glBegin (GL_LINE_LOOP);

  glColor3f (1.0, 1.0, 1.0);
  glLineWidth (2.0f);
  glVertex3f (timelineLeft, 0, 0.0f);
  glVertex3f (timelineLeft + timelineLength, 0, 0.0f);
  glVertex3f (timelineLeft + timelineLength, timelineWidth, 0.0f);
  glVertex3f (timelineLeft, timelineWidth, 0.0f);

  glEnd ();

  glPopAttrib ();
  glPopAttrib ();
  glPopMatrix ();
}


void ViewControl::handle_mouse (int button, int state, int x, int y)
{
//   if (button == GLUT_LEFT_BUTTON  && state == GLUT_DOWN)
//     glbpLightInputDriver->set_pos (x, y);
//   else
//     glbpLightInputDriver->set_pos (-1, -1);
}